package com.yoso.controller;


import cn.hutool.core.codec.Base64;
import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.yoso.auth.sdk.JwtUtil;
import com.yoso.auth.sdk.config.JwtConfigurationProperties;
import com.yoso.core.dto.ResponseResult;
import com.yoso.service.LogService;
import com.yoso.service.WebService;
import com.yoso.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/web")
public class Controller {
    @Resource
    private LogService logService;
    @Resource
    private WebService webService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private JwtConfigurationProperties jwtConfigurationProperties;

    @GetMapping("/log")
    public ResponseResult<Object> log() {
        logService.log();
        return ResponseResult.success();
    }

    @GetMapping("/async")
    public ResponseResult<Object> async() {
        logService.thread();
        return ResponseResult.success();
    }

    @PostMapping("/addUser")
    public ResponseResult<UserVo> addUser(@RequestBody @Validated UserVo userVo) {
        return ResponseResult.success(userVo);
    }

    @GetMapping("/sleep/{seconds}")
    public ResponseResult<Object> sleep(@PathVariable("seconds") Integer seconds) {
        webService.sleep(seconds);
        return ResponseResult.success();
    }
    @GetMapping("/sleep/clear")
    public ResponseResult<Object> clear() {
        webService.clear();
        return ResponseResult.success();
    }

    @GetMapping("/print")
    public ResponseResult<Object> print() {
        char[] a = new char[2];
        a[0] = '\uD83D';
        a[1] = '\uDC2E';
        String s = new String(a);
        log.info(s);
        String encode = Base64.encode(s);
        System.out.println("encode = " + encode);
        byte[] decode = Base64.decode(encode);

        return ResponseResult.success(new String(decode));
    }

    @GetMapping("/jwt")
    public ResponseResult<Object> jwt() {
        String issuer = jwtConfigurationProperties.getIssuer();
        String secret = jwtConfigurationProperties.getSecret();
        MapBuilder<String, String> builder = MapUtil.builder();
        Map<String, String> build = builder.put("name", "yoso").build();
        String token = jwtUtil.generate(build);
        for (String split : token.split("\\.")) {
            log.info(Base64.decodeStr(split));
        }
        return ResponseResult.success(token);
    }
}
